package com.tony.blog.server.controller.web;

import com.tony.blog.api.response.BaseResponse;
import com.tony.blog.api.response.StatusCode;
import com.tony.blog.api.utils.ValidatorUtil;
import com.tony.blog.model.entity.CommentEntity;
import com.tony.blog.model.request.*;
import com.tony.blog.model.entity.SysUserEntity;
import com.tony.blog.server.controller.AbstractController;
import com.tony.blog.server.service.web.IndexCenterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: IndexCenterController
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/15/21:50
 * @Description: 前端数据展示交互
 */
@RestController
@RequestMapping("web/center")
public class IndexCenterController extends AbstractController {
    
    @Autowired
    private IndexCenterService indexCenterService;
    
    /**
    * @Description: data 获取首页数据列表信息(向下滚动分页)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/15 21:55
    */
    @RequestMapping(value = "data",method = RequestMethod.GET)
    public BaseResponse data(@RequestParam Map<String,Object> paramMap) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.data(paramMap));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: myInfo 获取个人信息
    * @Param: []
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/19 19:54
    */
    @RequestMapping(value = "info/my",method = RequestMethod.GET)
    public BaseResponse myInfo() {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.getInfoByUserId(entity.getUserId()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: uploadImage 上传图片(每上传一张就调用一次本接口，
     *                          返回：图片的id和访问路径，再提交微博信息，
     *                          将图片的id用,隔开拼接到一个字符串字段一起提及过来即可)
    * @Param: [request]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/21 23:33
    */
    @RequestMapping(value = "image/upload",method = RequestMethod.POST)
    public BaseResponse uploadImage(MultipartHttpServletRequest request){
        if (null==getUser()){
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.uploadImg(request));

        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: senBlog 发微博
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/23 22:14
    */
    @PostMapping("blog/send")
    public BaseResponse senBlog(@RequestBody @Validated BlogRequest request, BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.sendBlog(request,entity.getUserId());
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: concernOn 关注
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/27 23:27
    */
    @PostMapping("concern/on")
    public BaseResponse concernOn(@RequestBody @Validated ConcernRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("插入的关注信息id为：{}",indexCenterService.concernOn(request,entity));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: concernCancel 取消关注
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/27 23:32
    */
    @PostMapping("concern/cancel")
    public BaseResponse concernCancel(@RequestBody @Validated ConcernRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.concernCancel(request,entity.getUserId());
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: praiseOn 点赞
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/30 15:59
    */
    @PostMapping("praise/on")
    public BaseResponse praiseOn(@RequestBody @Validated OperationRequest request,
                                 BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.praiseOn(request,entity));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: praiseCancel 取消点赞
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/30 22:16
    */
    @PostMapping("praise/cancel")
    public BaseResponse praiseCancel(@RequestBody @Validated OperationRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("取消点赞的状态为{}",indexCenterService.praiseCancel(request,entity.getUserId()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: collectOn 收藏
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/31 20:53
    */
    @PostMapping("collect/on")
    public BaseResponse collectOn(@RequestBody @Validated OperationRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.collectOn(request,entity.getUserId()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: collectCancel 取消收藏
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/1 22:08
    */
    @PostMapping("collect/cancel")
    public BaseResponse collectCancel(@RequestBody @Validated OperationRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.collectCancel(request,entity.getUserId());
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: commentOn 评论
    * @Param: []
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/4 22:56
    */
    @PostMapping("comment/on")
    public BaseResponse commentOn(@RequestBody @Validated CommentRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.commentOn(request,entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: deleteBlog 删除自己的微博
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/9 22:01
    */
    @PostMapping("blog/delete")
    public BaseResponse deleteBlog(@RequestBody @Validated IdRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.deleteSelfBlog(request,entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: replyOn 回复
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/10 11:12
    */
    @PostMapping("reply/on")
    public BaseResponse replyOn(@RequestBody @Validated ReplyRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.replyOn(request,entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: cpmments 获取博文评论列表
    * @Param: [blogId]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/10 11:39
    */
    @RequestMapping(value = "comments/blog",method = RequestMethod.GET)
    public BaseResponse cpmments(@RequestParam Integer blogId) {
        if (Objects.isNull(blogId) || blogId <= 0) {
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("----开始获取博文评论列表数据----");
            List<CommentEntity> comments = indexCenterService.getCommentsByBlog(blogId);
            log.info("获取到的博文回复列表数据为：{}",comments);
            response.setData(comments);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: forwardBlog 转发博文
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/10 14:18
    */
    @PostMapping("blog/forward")
    public BaseResponse forwardBlog(@RequestBody @Validated BlogForwardRequest request,
                                    BindingResult result) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.forwardBlog(request,entity);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: blogs 我的博文列表(分页)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/10 15:14
    */
    @RequestMapping(value = "blogs",method = RequestMethod.GET)
    public BaseResponse blogs(@RequestParam Map<String,Object> paramMap) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            paramMap.put("userId",entity.getUserId());
            response.setData(indexCenterService.getBlogByUserId(paramMap));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: myPraises 我点赞的博文信息(分页)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/10 16:01
    */
    @RequestMapping(value = "praises/my",method = RequestMethod.GET)
    public BaseResponse myPraises(@RequestParam Map<String,Object> paramMap) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            paramMap.put("userId",entity.getUserId());
            response.setData(indexCenterService.getMyPraisesBlog(paramMap));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: myCollects 我收藏的博文(分页)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/16 9:51
    */
    @RequestMapping(value = "collects/my",method = RequestMethod.GET)
    public BaseResponse myCollects(@RequestParam Map<String,Object> paramMap) {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            paramMap.put("userId",entity.getUserId());
            response.setData(indexCenterService.getMyCollectsBlog(paramMap));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: myFriends 我的朋友圈(关注我的人发的所有博文)
    * @Param: []
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/16 10:44
    */
    @RequestMapping(value = "friends/my",method = RequestMethod.GET)
    public BaseResponse myFriends() {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.getMyFriendsBlog(entity.getUserId()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: dataSearchInit 全站数据Lucene初始化(postman测试)
    * @Param: []
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/28 10:42
    */
    @RequestMapping(value = "data/search/init",method = RequestMethod.GET)
    public BaseResponse dataSearchInit() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            indexCenterService.initLuceneData();
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: dataSearch 首页中心数据列表(全文搜索)
    * @Param: [paramMap]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/28 14:08
    */
    @RequestMapping(value = "data/search",method = RequestMethod.GET)
    public BaseResponse dataSearch(@RequestParam Map<String,Object> paramMap) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexCenterService.dataSearchLucene(paramMap));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }






















}
